﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Meraz.Core.Model;
using Meraz.Core.Managers;
using Meraz.Web.Models.Domains;
using Meraz.Web.Models;
using Ninject;
using System.IO;
using System.Diagnostics;
using System.Text;
using Meraz.Web.Filters;

namespace Meraz.Web.Controllers
{
    public class DomainController : Controller
    {
        [Inject]
        public DomainManager DomainManager{ get; set; }

        //
        // GET: /Domain/

        public ActionResult Index()
        {
            DomainsModel model = new DomainsModel();
            model.Domains = DomainManager.Get(10, 0);
            return View(model);
        }


        public ActionResult Page(int? id)
        {
            int index = id ?? 0;
            DomainsModel model = new DomainsModel();
            model.Domains = DomainManager.Get(10, index);
            return View("Index", model);
        }


        public ActionResult Twitter()
        {
            List<Domain> domains = DomainManager.GetTwitterDomains();
            StringBuilder builder = new StringBuilder();
            using (StringWriter writer = new StringWriter(builder)){
                writer.WriteLine("[");
                foreach (Domain domain in domains)
                    writer.WriteLine(domain.ToJavascriptArray());
                writer.WriteLine("]");
            }
            return Content(builder.ToString());
        }


        [ObjectFilter(Param = "domain", RootType = typeof(Domain))]
        [HttpPost]
        public ActionResult Save(Domain domain)
        {
            List<Domain> list = new List<Domain>(){domain};
            DomainManager.Save(list);
            RowModel model = new RowModel();
            model.Values = new List<string>();
            model.Values.Add(domain.ID.ToString());
            model.Values.Add(domain.Name);
            model.Values.Add(domain.Ideology);
            model.Values.Add(domain.Media);
            model.Values.Add(domain.Organization);
            model.Values.Add(domain.Party);
            model.Values.Add(domain.Social);
            model.Values.Add(domain.Theme);
            model.Message = "Domain saved!";
            return Json(model);
        }

    }
}
